// var mydate=new Date();
// var myddy=date.getDay();//获取存储当前日期
// var weekday=["周日","周一","周二","周三","周四","周五","周六"];
// document.write("今天是：" + weekday[myddy]);
// 毫秒值，根据不同的参数获取年月日时分秒
// 参数(毫秒值，int) int取值为:0:年1:月2:日3:时4:分5:秒
export const getTime = function(second, getDateType) {
  let weekday = ["(周日)", "(周一)", "(周二)", "(周三)", "(周四)", "(周五)", "(周六)"];
  let date = new Date(second);
  if (getDateType == 0) {
    return date.getFullYear();
  } else if (getDateType == 1) {
    if (date.getMonth() + 1 <= 9) {
      return "0" + (date.getMonth() + 1);
    } else {
      return date.getMonth() + 1;
    }
  } else if (getDateType == 2) {
    if (date.getDate() <= 9) {
      return "0" + date.getDate();
    } else {
      return date.getDate();
    }
  } else if (getDateType == 3) {
    if (date.getHours() <= 9) {
      return "0" + date.getHours();
    } else {
      return date.getHours();
    }
  } else if (getDateType == 4) {
    if (date.getMinutes() <= 9) {
      return "0" + date.getMinutes();
    } else {
      return date.getMinutes();
    }
  } else if (getDateType == 5) {
    return date.getSeconds();
  } else if (getDateType == 6) {
    let myday = date.getDay();
    return weekday[myday];
  } else {
    console.log("输入时间格式有误!");
    return;
  }
};

export function parseTime(time, cFormat) {
  if (arguments.length === 0 || !time) {
    return ''
  }
  const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
  let date
  if (typeof time === 'object') {
    date = time
  } else {
    if ((typeof time === 'string') && (/^[0-9]+$/.test(time))) {
      time = parseInt(time)
    }
    if ((typeof time === 'number') && (time.toString().length === 10)) {
      time = time * 1000
    }
    date = new Date(time)
  }
  const formatObj = {
    y: date.getFullYear(),
    m: date.getMonth() + 1,
    d: date.getDate(),
    h: date.getHours(),
    i: date.getMinutes(),
    s: date.getSeconds(),
    a: date.getDay()
  }
  const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
    let value = formatObj[key]
    // Note: getDay() returns 0 on Sunday
    if (key === 'a') { return ['日', '一', '二', '三', '四', '五', '六'][value ] }
    if (result.length > 0 && value < 10) {
      value = '0' + value
    }
    return value || 0
  })
  return time_str
}